#include "LinkQueue.h"
#include "LinkStack.h"
#include <string>

struct Car
{
    Car() {}
    Car(const std::string &name, int arriveTime) : name(name), arriveTime(arriveTime) {}
    std::string name; // 车牌号
    int arriveTime;   // 到达时间
    int leaveTime;    // 离开时间
};

class CarStop
{
public:
    CarStop(const int &size) : size(size) {}
    // 车来
    void ComeCar();
    // 车走
    void LeaveCar();

    void show();

private:
    LinkStack stop;      // 停车场
    LinkQueque pavement; // 过道

    const int size; // 停车场停车数量
};

void CarStop::ComeCar()
{
    Car *c = new Car;
    std::cout << "请输入它的车牌:";
    std::cin >> c->name;
    std::cout << "请输入它的到达时间:";
    std::cin >> c->arriveTime;

    if (stop.Size() == size) // 停车场满了
    {
        pavement.Push(c);
        std::cout << "停车场位置已满，车牌:" << c->name << "停在了过道上" << std::endl;
    }
    else
    {
        stop.Push(c);
        std::cout << "车辆" << c->name << "停在了停车场内" << std::endl;
    }
}

void CarStop::LeaveCar()
{
    int leavetime = 0;
    LinkStack tmp; // 临时过道
    std::cout << "请输入要离场的车牌:";
    std::string name;
    std::cin >> name;

    while (!stop.IsEmpty())
    {
        Car *c = (Car *)*stop.GetTop();
        if (c->name == name)
        {
            std::cout << "请输入离开时间";
            std::cin >> c->leaveTime;
            leavetime = c->leaveTime;
            int money = (c->leaveTime - c->arriveTime) * 20;
            std::cout << "车辆" << c->name << "于" << c->leaveTime << "离开,缴费:" << money << std::endl;
            delete c;
            stop.Pop();
            break;
        }
        stop.Pop();
        tmp.Push(c);
    }
    while (!tmp.IsEmpty())
    {
        stop.Push(*tmp.GetTop());
        tmp.Pop();
    }

    // 停车场不满，且过道上有车
    if (stop.Size() < size && !pavement.IsEmpty())
    {
        Car *c = (Car *)*pavement.GetFront();
        c->arriveTime = leavetime;
        std::cout << "过道上的车" << c->name << "在" << c->arriveTime << "进入停车场\n";
        pavement.Pop();
        stop.Push(c);
    }
}

void CarStop::show()
{
    LinkStack tmp;
    std::cout << "停车场:" << std::endl;
    while (!stop.IsEmpty())
    {
        tmp.Push(*stop.GetTop());
        stop.Pop();
    }
    while (!tmp.IsEmpty())
    {
        Car *c = (Car *)*tmp.GetTop();
        std::cout << c->name << "  到达时间" << c->arriveTime << std::endl;
        stop.Push(c);
        tmp.Pop();
    }

    std::cout << "过道:" << std::endl;
    LinkQueque q;
    while (!pavement.IsEmpty())
    {
        Car *c = (Car *)*pavement.GetFront();
        std::cout << c->name << std::endl;
        pavement.Pop();
        q.Push(c);
    }
    while (!q.IsEmpty())
    {
        pavement.Push(*q.GetFront());
        q.Pop();
    }
}

int main()
{
    CarStop c(3);
    int Ops = -1;
    std::cout << "请输入你要进行的操作:" << std::endl;
    std::cout << "1、来车\n"
              << "2、离开\n"
              << "3、显示\n"
              << "4、退出系统" << std::endl;
    while (1)
    {
        std::cin >> Ops;
        switch (Ops)
        {
        case 1:
            c.ComeCar();
            break;
        case 2:
            c.LeaveCar();
            break;
        case 3:
            c.show();
            break;
        case 4:
            exit(-1);
        default:
            break;
        }
    }
    return 0;
}
